var loginService = require('../../service/loginService');

module.exports = {
    get: {
        out: function (req, res) {
            req.session.removeAll();
            res.redirect('/');
        }
    },

    post: {
        exec: function (req, res) {
            var param = {};
            param.account = req.form.account;
            param.password = req.form.password;
            loginService.match(param, function (err, rows) {
                if (err || rows.length == 0) {
                    res.send({
                        result: 'fail',
                        msg: '用户名或密码错误'
                    });
                    return;
                }

                req.session.set("userId", rows[0].id);
                req.session.set("account", rows[0].account);
                req.session.set("userName", rows[0].userName);
                req.session.set("roleId", rows[0].roleId);
                req.session.set("powerList", rows[0].rolePath);
                req.session.set("factoryId", rows[0].factoryId);
                req.session.set("deptId", rows[0].deptId);
                req.session.set("factoryDes", rows[0].factoryDes);

                res.send({
                    result: 'ok',
                    msg: '登录成功'
                });
            });
        }
    }
};